package com.hdu.seckilldemo.vo;

import com.hdu.seckilldemo.utils.ValidatorUtil;
import com.hdu.seckilldemo.validator.IsMobile;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

//手机号码校验规则
public class isMobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required = false; //是否需要非空判断
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isMobile(s);
        }else{
            if(StringUtils.isEmpty(s)) return true;
            else{
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
